home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Microsoft Visual Basic .NET
/
Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin
/
17 controls
/
databindingdemo
/
simplebindingform.vb
< prev
next >
Wrap
Text File
|
2002-03-16
|
10KB
|
250 lines
Public Class SimpleBindingForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents txtFirstName As System.Windows.Forms.TextBox
Friend WithEvents lblRecord As System.Windows.Forms.Label
Friend WithEvents btnPrevious As System.Windows.Forms.Button
Friend WithEvents btnNext As System.Windows.Forms.Button
Friend WithEvents btnLast As System.Windows.Forms.Button
Friend WithEvents btnFirst As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtLastName As System.Windows.Forms.TextBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtBirthDate As System.Windows.Forms.TextBox
Friend WithEvents chkPartTime As System.Windows.Forms.CheckBox
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtBirthDate = New System.Windows.Forms.TextBox()
Me.btnLast = New System.Windows.Forms.Button()
Me.txtFirstName = New System.Windows.Forms.TextBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.btnPrevious = New System.Windows.Forms.Button()
Me.Label3 = New System.Windows.Forms.Label()
Me.btnFirst = New System.Windows.Forms.Button()
Me.btnNext = New System.Windows.Forms.Button()
Me.lblRecord = New System.Windows.Forms.Label()
Me.txtLastName = New System.Windows.Forms.TextBox()
Me.chkPartTime = New System.Windows.Forms.CheckBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'txtBirthDate
'
Me.txtBirthDate.Location = New System.Drawing.Point(16, 112)
Me.txtBirthDate.Name = "txtBirthDate"
Me.txtBirthDate.Size = New System.Drawing.Size(192, 24)
Me.txtBirthDate.TabIndex = 5
Me.txtBirthDate.Text = ""
'
'btnLast
'
Me.btnLast.Location = New System.Drawing.Point(320, 168)
Me.btnLast.Name = "btnLast"
Me.btnLast.Size = New System.Drawing.Size(48, 32)
Me.btnLast.TabIndex = 11
Me.btnLast.Text = ">>"
'
'txtFirstName
'
Me.txtFirstName.Location = New System.Drawing.Point(16, 48)
Me.txtFirstName.Name = "txtFirstName"
Me.txtFirstName.Size = New System.Drawing.Size(192, 24)
Me.txtFirstName.TabIndex = 1
Me.txtFirstName.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(168, 24)
Me.Label1.TabIndex = 0
Me.Label1.Text = "First Name"
'
'btnPrevious
'
Me.btnPrevious.Location = New System.Drawing.Point(136, 168)
Me.btnPrevious.Name = "btnPrevious"
Me.btnPrevious.Size = New System.Drawing.Size(48, 32)
Me.btnPrevious.TabIndex = 8
Me.btnPrevious.Text = "<"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 88)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(168, 24)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Birth Date"
'
'btnFirst
'
Me.btnFirst.Location = New System.Drawing.Point(88, 168)
Me.btnFirst.Name = "btnFirst"
Me.btnFirst.Size = New System.Drawing.Size(48, 32)
Me.btnFirst.TabIndex = 7
Me.btnFirst.Text = "<<"
'
'btnNext
'
Me.btnNext.Location = New System.Drawing.Point(272, 168)
Me.btnNext.Name = "btnNext"
Me.btnNext.Size = New System.Drawing.Size(48, 32)
Me.btnNext.TabIndex = 10
Me.btnNext.Text = ">"
'
'lblRecord
'
Me.lblRecord.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.lblRecord.Location = New System.Drawing.Point(184, 168)
Me.lblRecord.Name = "lblRecord"
Me.lblRecord.Size = New System.Drawing.Size(88, 32)
Me.lblRecord.TabIndex = 9
Me.lblRecord.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'txtLastName
'
Me.txtLastName.Location = New System.Drawing.Point(232, 48)
Me.txtLastName.Name = "txtLastName"
Me.txtLastName.Size = New System.Drawing.Size(192, 24)
Me.txtLastName.TabIndex = 3
Me.txtLastName.Text = ""
'
'chkPartTime
'
Me.chkPartTime.Location = New System.Drawing.Point(232, 112)
Me.chkPartTime.Name = "chkPartTime"
Me.chkPartTime.Size = New System.Drawing.Size(176, 16)
Me.chkPartTime.TabIndex = 6
Me.chkPartTime.Text = "PartTime Employee"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(232, 24)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(168, 24)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Last Name"
'
'SimpleBindingForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
Me.ClientSize = New System.Drawing.Size(456, 213)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.chkPartTime, Me.Label3, Me.txtBirthDate, Me.Label2, Me.txtLastName, Me.lblRecord, Me.btnLast, Me.btnNext, Me.btnPrevious, Me.btnFirst, Me.txtFirstName, Me.Label1})
Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "SimpleBindingForm"
Me.Text = "Simple Data Binding"
Me.ResumeLayout(False)
End Sub
#End Region
' an array of object that works as a data source
Dim employees() As Employee = {New Employee("Joe", "Doe", #1/3/1960#), _
New Employee("Robert", "Smith", #11/23/1962#), _
New Employee("Ann", "Ross", #2/5/1965#, True)}
Dim WithEvents bmb As BindingManagerBase
' initialize data binding when the form loads
Private Sub SimpleBindingForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitializeBinding()
End Sub
Sub InitializeBinding()
' Create the Binding object for each bound field.
txtFirstName.DataBindings.Add("Text", employees, "FirstName")
txtLastName.DataBindings.Add("Text", employees, "LastName")
' Get a reference to this binding and bind it to two events.
Dim bnd As Binding = txtBirthDate.DataBindings.Add("Text", employees, "BirthDate")
AddHandler bnd.Format, AddressOf DataFormat
AddHandler bnd.Parse, AddressOf DataParse
chkPartTime.DataBindings.Add("Checked", employees, "PartTimeEmployee")
' Save a reference to the BindingManagerBase object.
bmb = Me.BindingContext(employees)
' Force a refresh of buttons
bmb.Position = bmb.Count
bmb.Position = 0
End Sub
' navigational buttons
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
bmb.Position = 0
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
bmb.Position -= 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
bmb.Position += 1
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
bmb.Position = bmb.Count - 1
End Sub
' Display current position in label.
Private Sub bmb_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles bmb.PositionChanged
lblRecord.Text = String.Format("{0} of {1}", bmb.Position + 1, bmb.Count)
' Enable/disable buttons.
btnFirst.Enabled = (bmb.Position > 0)
btnPrevious.Enabled = (bmb.Position > 0)
btnNext.Enabled = (bmb.Position < bmb.Count - 1)
btnLast.Enabled = (bmb.Position < bmb.Count - 1)
End Sub
' an example of how you can format values when they're taken
' from the data source
Private Sub DataFormat(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs)
If sender Is txtBirthDate Then
e.Value = String.Format("{0:dd-MM-yyyy}", e.Value)
End If
End Sub
' an example of how you can parse data before it's stored back
' in the data source
Private Sub DataParse(ByVal sender As Object, ByVal e As System.Windows.Forms.ConvertEventArgs)
If sender Is txtBirthDate Then
e.Value = CDate(e.Value)
End If
End Sub
End Class